package com.fruit.alisms.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.fruit.pojo.sms.SmsTemplet;

import net.sf.json.JSONObject;

/**
 * 阿里短信
 * 
 * @author Administrator
 *
 */
public class AliSms {
	
	private final static String product = "Dysmsapi";
	protected static Logger logger = LoggerFactory.getLogger(AliSms.class);


	public static String sendCode(SendSmsRequest request,SmsTemplet st) {
		SendSmsResponse sendSmsResponse=null;
		String result="";
		try {
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", st.getAppKey(), st.getAppSecret());
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, st.getSendUrl());
			IAcsClient acsClient = new DefaultAcsClient(profile);
            request.setMethod(MethodType.POST);
            request.setSignName(st.getSign());
            request.setTemplateCode(st.getTempletId());
            sendSmsResponse= acsClient.getAcsResponse(request);
            if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            	result="success";
            }else{
            	result="error";
            }
		} catch (ClientException e) {
			e.printStackTrace();
			logger.info("发送短信---【异常】---"+e.getMessage());
			result="error";
		}
		return result;
	}
}
